#include <stats.h>

#include <assert.h>
#include <stdio.h>
#include <inttypes.h>


#ifndef NDEBUG
static int init = 0;
#endif

int64_t stat_counters[stat_last];


static struct {
  const char* label;
  int percentage;
} stat_info[stat_last] = {
  {"(none)", 0},
  {"Nodes searched", 0},
  {"Interrupted nodes", stat_nodes},
  {"Terminal nodes", stat_nodes},
  {"Non-terminal nodes", stat_nodes},
  {"Interior nodes", stat_nodes},
  {"PV nodes", stat_nonterminal_nodes},
  {"PV best move", stat_alpha_nodes},
  {"Alpha nodes", stat_nonterminal_nodes},
  {"Beta nodes", stat_nonterminal_nodes},
  {"TT nodes", stat_nonterminal_nodes},
  {"TT best move", stat_alpha_nodes},
  {"TT move beta", stat_nonterminal_nodes},
  {"IID searches", stat_nonterminal_nodes},
  {"IID best move", stat_iid_searches},
  {"IID move beta", stat_iid_searches},
  {"Null move tests", stat_nonterminal_nodes},
  {"Null move pruning", stat_null_tests},
  {"Futility pruning", stat_nonterminal_nodes},
  {"Extended nodes", stat_nonterminal_nodes},
  {"Late move reductions", stat_nonterminal_nodes},
  {"TT probes", 0},
  {"TT hits", stat_hash_probe},
  {"Pawn table probes", 0},
  {"Pawn table hits", stat_pawn_hash_probe},
  {"Material table probes", 0},
  {"Material table hits", stat_material_hash_probe}
};


void stat_init()
{
  for (int i = 0; i < stat_last; i++)
    stat_counters[i] = 0;
  assert((init = 1));
}


void stat_print()
{
#if LAGGARD_STATS
  assert(init);
  for (int i = 1; i < stat_last; i++) {
    printf("# %-20s\t%8" PRId64, stat_info[i].label, stat_counters[i]);
    int p = stat_info[i].percentage;
    if (p != stat_none) {
      if (stat_counters[p] == 0) {
	printf(" \t(-)");
      } else {
	double d = (100.0 * (stat_counters[i]));
	d /= stat_counters[p];
	printf(" \t(%g%%)", d);
      }
    }
    printf("\n");
  }
#endif
}
